package com.isunland.managesystem.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;
import com.google.gson.Gson;
import com.isunland.managesystem.R;
import com.isunland.managesystem.adapter.StageRegistListAdapter;
import com.isunland.managesystem.base.BaseListFragment;
import com.isunland.managesystem.common.ApiConst;
import com.isunland.managesystem.entity.CustomerDialog;
import com.isunland.managesystem.entity.StageRegisterOriginal;
import java.util.ArrayList;
import java.util.HashMap;

/**
 * 阶段进度登记列表
 * Created by lenovo on 2016/7/4.
 */
public class StageRegistListFragment extends BaseListFragment {
  /* 阶段ID*/
  public static final String EXTRA_SAGE_ID =
      "com.isunland.managesystem.ui.StageDefinePropertyListFragment.EXTRA_SAGE_ID";
  public static final String EXTRA_SAGE_NAME =
      "com.isunland.managesystem.ui.StageDefinePropertyListFragment.EXTRA_SAGE_NAME";
  public static final String EXTRA_CONTENT =
      "com.isunland.managesystem.ui.StageDefinePropertyListFragment.EXTRA_CONTENT";
  private static final int DETAIL = 0;
  private StageRegistListAdapter mAdapter;
  private CustomerDialog extContent;

  @Override public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    extContent = (CustomerDialog) getArguments().getSerializable(EXTRA_CONTENT);
  }

  @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    //        if ("new".equals(extContent.getJobNo()) || "newback".equals(extContent.getJobNo()) ) {
    inflater.inflate(R.menu.menu_add, menu);
    //        }else {
    //            super.onCreateOptionsMenu(menu, inflater);
    //        }

  }

  @Override public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
      case R.id.menu_item_add:
        //                if ((mUser.getJobNumber()).equals(extContent.getId()) ||(mUser.getJobNumber()).equals(extContent.getName())) {

        //                    if ("new".equals(extContent.getJobNo()) || "newback".equals(extContent.getJobNo()) ) {

        Intent intent = new Intent(getActivity(), StageRegisterDetailActivity.class);
        intent.putExtra(EXTRA_CONTENT, extContent);
        startActivityForResult(intent, DETAIL);
        //                    }else {
        //                        ToastUtil.show("只有草稿和返回修改状态才能修改！");
        //                    }

        //                }else {
        //
        //                    ToastUtil.show(R.string.pm_regist);
        //                }

        break;
    }
    return super.onOptionsItemSelected(item);
  }

  @Override public String getUrl() {
    return ApiConst.ULR_STAGE_REGIST_LIST;
  }

  @Override public HashMap<String, String> getparameters() {
    HashMap<String, String> params = new HashMap<>();
    params.put("stageId", extContent.getMemberCode());
    params.put("memberCode", mCurrentUser.getMemberCode());
    params.put("orderField", "ORDER_NO");
    params.put("orderSeq", "asc");
    return params;
  }

  @Override public void resolveResponse(String json) {
    StageRegisterOriginal original = new Gson().fromJson(json, StageRegisterOriginal.class);
    ArrayList<StageRegisterOriginal.StageRegisterContent> rows = original.getRows();
    setAdapter(rows);
  }

  private void setAdapter(ArrayList<StageRegisterOriginal.StageRegisterContent> rows) {
    if (rows == null) {
      rows = new ArrayList<>();
    }
    mAdapter = new StageRegistListAdapter(mActivity, rows);
    setListAdapter(mAdapter);
  }

  @Override public void onListItemClick(ListView l, View v, int position, long id) {
    StageRegisterOriginal.StageRegisterContent content = mAdapter.getItem(position - 1);
    Intent intent = new Intent(getActivity(), StageRegisterDetailActivity.class);
    intent.putExtra(StageRegisterDetailFragment.EXTRA_CONTENT, content);
    intent.putExtra(EXTRA_CONTENT, extContent);
    //        intent.putExtra(EXTRA_SAGE_ID,stageId);
    //        intent.putExtra(EXTRA_SAGE_NAME,stageName);

    startActivityForResult(intent, DETAIL);
    super.onListItemClick(l, v, position, id);
  }

  @Override public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode != Activity.RESULT_OK) {
      return;
    }
    if (requestCode == DETAIL) {
      volleyPost();
    }
  }

  public static StageRegistListFragment newInstence(String stageId, String stageName) {
    Bundle bundle = new Bundle();
    bundle.putSerializable(EXTRA_SAGE_ID, stageId);
    bundle.putSerializable(EXTRA_SAGE_NAME, stageName);
    StageRegistListFragment mfragment = new StageRegistListFragment();
    mfragment.setArguments(bundle);
    return mfragment;
  }

  public static StageRegistListFragment newInstence(CustomerDialog content) {
    Bundle bundle = new Bundle();
    bundle.putSerializable(EXTRA_CONTENT, content);
    StageRegistListFragment mfragment = new StageRegistListFragment();
    mfragment.setArguments(bundle);
    return mfragment;
  }
}
